package com.hxc;

import com.hxc.dao.UserDataDao;
import com.hxc.user.UserData;
import java.sql.Connection;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserService {

  public UserData findUser(Connection conn, String userId) throws Exception {
    try {
      UserDataDao userDataDao = new UserDataDao(conn);
      UserData userData = userDataDao.selectByUserId(userId);
      if (null == userData) {
        throw new Exception("查无用户:" + userId);
      }
      return userData;
    } catch (Exception e) {
      throw new Exception("获取用户失败:" + e);
    }
  }

  public String insertUser(Connection conn, UserData userData) throws Exception {
    try {
      UserDataDao userDataDao = new UserDataDao(conn);
      if (!StringUtils.hasText(userData.getUserId())) {
        throw new Exception("用户id不能为空!");
      }
      if (!StringUtils.hasText(userData.getNickName())) {
        throw new Exception("用户名称不能为空!");
      }
      UserData record = userDataDao.selectByUserId(userData.getUserId());
      if (null != record) {
        throw new Exception("用户:" + userData.getUserId() + "已存在");
      }
      userDataDao.insert(userData);

      return "新增成功!";
    } catch (Exception e) {
      throw new Exception("新增用户失败:" + e);
    }
  }
}
